﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SharedComponents;

namespace OurLeague.Object
{
    public class Coach : Person
    {
        #region Fields

        public static List<Coach> Coaches = new List<Coach>();

        #endregion

        #region Constructors

        public Coach() : base() { }
        public Coach(Person person) : base(person) { }

        #endregion

        #region Public Methods

        public new void delete()
        {
            base.delete();
            Coaches.Remove(this);
        }

        #endregion

        #region Properties
        #endregion

        #region Static methods

        public static new void loadAll()
        {
            var queryResult = MySqlQueryHelper.selectQuery(MySqlManager.Connection, "Coaches", null, null);
            foreach (MySqlRecord coachRecord in queryResult)
            {
                if (Coaches.Find(p => p.Id == coachRecord["coachId"].ToString()) == null)
                    addNewCoach(coachRecord);
            }
        }

        private static Coach addNewCoach(MySqlRecord coachRecord)
        {
            Person person = Person.load(coachRecord["coachId"].ToString());
            Coach coach = new Coach(person);
            Person.Persons[Person.Persons.FindIndex(P => P.Id == coach.Id)] = coach;

            // raffinamento di coach se necessario

            Coach.Coaches.Add(coach);

            return coach;
        }

        public new static Coach load(String idCoach)
        {
            Coach coach = Coaches.Find(r => r.Id == idCoach);

            if (coach == null)
            {
                var queryResult = MySqlQueryHelper.selectQuery(MySqlManager.Connection, "Coaches", null, "coachId=" + idCoach);
                coach = addNewCoach(queryResult[0]);
            }

            return coach;
        }

        #endregion
    }
}
